package General;


import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;

import Data.Coordinates;
import Data.Tracks;
import android.os.AsyncTask;
import android.os.Environment;

public class ReadFromFile extends AsyncTask<String, String, Tracks>{

	private String TrackName;
	private Tracks Track_in_Use;
	
	public ReadFromFile(String TrackN){
		TrackName = TrackN;
		execute();
	}
	
	@Override
	protected Tracks doInBackground(String... arg0) {
		// TODO Auto-generated method stub
		
				
				Track_in_Use = new Tracks(TrackName, TrackName);
				int YEAR=-1, MONTH=-1,DAY=-1,HOUR=-1,MINUTE=-1,SECOND=-1;
				double LATITUDE=-1,LONGITUDE=-1,ALTITUDE=-1,SPEED=-1;
				String [] Str;
				
				//open and reading from file
				final StringBuffer storedString = new StringBuffer();
				File dir = Environment.getExternalStorageDirectory();
				File Map = new File(dir, "/TrackingData/"+TrackName);
				

				try {
					FileInputStream Fis = new FileInputStream(Map);
				    DataInputStream dataIO = new DataInputStream(Fis);
				    String strLine = null;

				    strLine = dataIO.readLine(); //reads header's line

				   //read continuosly until EOF!
				    do
				    {
				    	if ((strLine = dataIO.readLine()) != null) {
				    		Str = strLine.split(","); //separate the string by ","
				    		
				    		for(int i=0 ; i< Str.length ; i++){ //GET each field
				    			switch(i){
				    			case 0:
				    				YEAR = Integer.parseInt(Str[i]);
				    				break;
				    			case 1:
				    				MONTH = Integer.parseInt(Str[i]);
				    				break;
				    			case 2:
				    				DAY = Integer.parseInt(Str[i]);
				    				break;
				    			case 3:
				    				HOUR = Integer.parseInt(Str[i]);
				    				break;
				    			case 4:
				    				MINUTE = Integer.parseInt(Str[i]);
				    				break;
				    			case 5:
				    				SECOND = Integer.parseInt(Str[i]);
				    				break;
				    			case 6:
				    				LATITUDE = Double.parseDouble(Str[i]);
				    				break;
				    			case 7:
				    				LONGITUDE = Double.parseDouble(Str[i]);
				    				break;
				    			case 8:
				    				ALTITUDE = Double.parseDouble(Str[i]);
				    				break;
				    			case 9:
				    				SPEED = Double.parseDouble(Str[i]);
				    				break;
				    				
				    			}
				    		}
				    		
				    		//creating new Coordinate
				    		Coordinates NewCoordinate = new Coordinates(YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, LATITUDE, LONGITUDE, ALTITUDE, SPEED);
				    		
				    		//adding new coordinate to track
				    		Track_in_Use.SetNewCoordinate(NewCoordinate);
				    		
				    		
					    }
				    	
				    	
				    }while((strLine = dataIO.readLine()) != null);
				    	
				   
				    dataIO.close();
				    Fis.close();
				}
				catch  (Exception e) {
					e.printStackTrace();
				}
				
		return Track_in_Use;
	}
	
	 protected Tracks onPostExecute(Long result) {
         return Track_in_Use;
     }

}
